using UnityEngine;

public enum BirdState
{
    Waiting,//等待
    BeforeShoot,//发射前
    AfterShoot,//发射后
    WaitToDie//死亡
}

public class Bird : MonoBehaviour
{
    public BirdState state = BirdState.BeforeShoot;
    private bool isMouseDown = false; //是否按下

    public float maxDistance = 1.5f;//限制拉动距离
    protected Rigidbody2D rb;
    public float flySpeed;//飞行速度
    bool isFlying;//是否飞行
    bool isUserdSkill;//是否已使用技能
    private Trajectory trajectory;// 轨迹预测器
    public float force = 10f;//力大小
    private Vector2 m_pushSpeed;//力向量

    private void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        rb.bodyType = RigidbodyType2D.Static;
        trajectory = FindObjectOfType<Trajectory>();
    }

    void Update()
    {
        switch (state)
        {
            case BirdState.Waiting:
                break;
            case BirdState.BeforeShoot:
                MoveControl();
                break;
            case BirdState.AfterShoot:
                StopControl();
                SkillControl();
                break;
            case BirdState.WaitToDie:
                break;
            default:
                break;
        }
    }

    //按下触发事件
    private void OnMouseDown()
    {
        if (state == BirdState.BeforeShoot)
        {
            isMouseDown = true;
            Slingshot.Instance.StartDraw(transform);
            // 显示轨迹
            trajectory.Show();
        }
    }

    //抬起触发事件
    private void OnMouseUp()
    {
        if (state == BirdState.BeforeShoot)
        {
            AudioManager.Instance.PlaySFX("发射");
            isMouseDown = false;
            Slingshot.Instance.EndDraw();
            Fly();
            isFlying = true;
            // 隐藏轨迹
            trajectory.Hide();
            GetComponent<TestMyTrail>().heroAttack();
        }
    }

     
    private void MoveControl()
    {
        if (isMouseDown)
        {
            transform.position = GetMousePosition();//跟随鼠标

            Vector3 mouseDir = Slingshot.Instance.shootPoint.position - transform.position;
            m_pushSpeed = mouseDir.normalized * mouseDir.magnitude * force;//力向量
            trajectory.UpdateDots(transform.position, m_pushSpeed);//更新预测点坐标
        }
    }


    //屏幕坐标转世界坐标
    private Vector3 GetMousePosition()
    {
        Vector3 position = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        position.z = 0;
        // 获取弹弓的中心位置
        Vector3 centerPosition = Slingshot.Instance.shootPoint.position;
        //计算鼠标指向的方向
        Vector3 mouseDir = position - centerPosition;
        //计算鼠标指向的距离
        float distance = mouseDir.magnitude;
        // 如果距离超过最大距离，则限制在最大距离范围内
        if (distance > maxDistance)
        {
            position = mouseDir.normalized * maxDistance + centerPosition;
        }
        return position;
    }

    void Fly()
    {
        rb.bodyType = RigidbodyType2D.Dynamic;
        //按拉的距离给定飞行速度
        // Vector3 mouseDir = Slingshot.Instance.shootPoint.position - transform.position;
        // rb.velocity = mouseDir.magnitude / maxDistance * flySpeed * mouseDir.normalized;
        rb.AddForce(m_pushSpeed, ForceMode2D.Impulse);
        state = BirdState.AfterShoot;
    }

    //设置开始小鸟
    public void SetStart(Vector3 position)
    {
        state = BirdState.BeforeShoot;
        transform.position = position;
        AudioManager.Instance.PlaySFX("选择");
    }

    void StopControl()
    {
        if (rb.velocity.magnitude < 0.1f)
        {
            state = BirdState.WaitToDie;
            Invoke("LoadNextBird", 1f);
        }
    }

    //加载下一只鸟
    protected void LoadNextBird()
    {
        Destroy(gameObject);
        //爆炸特效
        GameObject prefabs = Resources.Load("Prefabs/VFX/爆炸烟雾特效") as GameObject;
        GameObject go = Instantiate(prefabs, transform.position, Quaternion.identity);
        Destroy(go, 1f);
        GameManager.Instance.LoadNextBird();
    }

    //使用技能
    void SkillControl()
    {
        if (isUserdSkill) return;

        if (Input.GetMouseButtonDown(0))
        {

            isUserdSkill = true;

            if (isFlying == true)
            {
                FlytingSkill();
            }
            FullTimeSkill();
        }
    }

    //飞行技能
    protected virtual void FlytingSkill()
    {

    }

    //全时段技能
    protected virtual void FullTimeSkill()
    {

    }

    private void OnCollisionEnter2D(Collision2D other)
    {
        if (state == BirdState.AfterShoot)
        {
            isFlying = false;
            AudioManager.Instance.PlaySFX("撞鸟");
        }
    }
}

